<?php
require "common.php";

$name = "taokejidi"; //项目名字
$houzui = "/index.html";
$url = "http://vip.tkjidi.com/";

$rootweb = mkdirmulu($name); //检测主目录是否存在

$html = curl_request($url); //获取第一页

$regaxcss = "/href=\"(.*?)\.css.*?\"/";
$regaxjs = "/src=\"(.*?)\.js.*?\"/";
$regaxjpg = "/src=\"(.*?)\.jpg.*?\"/";
$regaxpng = "/src=\"(.*?)\.png.*?\"/";
$regaxgif = "/src=\"(.*?)\.gif.*?\"/";


// css 开始
$regcss = zhengzetiqu($html, $regaxcss);
for ($i = 0; $i < count($regcss[0]); $i++) {
    $vo = getBetween($regcss[0][$i], "\"", "\"");
    if (strpos($vo, "http") === 0) {
        $temp = curl_request($vo);
        $filename = strrchr($regjs[1][$i], "/") . ".js";

        file_put($rootweb, "/css", $filename, $temp);

        $html = str_replace($vo, "/css" . $filename, $html);

    } else {
        $temp = curl_request($url . $vo);
        $filename = strrchr($regcss[1][$i], "/") . ".css";

        file_put($rootweb, "/css", $filename, $temp);

        $html = str_replace($vo, "/css" . $filename, $html);
    }
}

// js开始
$regjs = zhengzetiqu($html, $regaxjs);
for ($i = 0; $i < count($regjs[0]); $i++) {
    $vo = getBetween($regjs[0][$i], "\"", "\"");
    if (strpos($vo, "http") === 0) {
        $temp = curl_request($vo);

        $filename = strrchr($regjs[1][$i], "/") . ".js";

        file_put($rootweb, "/js", $filename, $temp);

        $html = str_replace($vo, "/js" . $filename, $html);

    } else {
        $temp = curl_request($url . $vo);
        $filename = strrchr($regjs[1][$i], "/") . ".js";

        file_put($rootweb, "/js", $filename, $temp);

        $html = str_replace($vo, "/js" . $filename, $html);


    }
}


// jpg开始
$regjpg = zhengzetiqu($html, $regaxjpg);
$regjpg[0] = array_unique($regjpg[0]);
$regjpg[1] = array_unique($regjpg[1]);
for ($i = 0; $i < count($regjpg[0]); $i++) {
    $vo = getBetween($regjpg[0][$i], "\"", "\"");
    if (strpos($vo, "http") === 0) {
        $temp = curl_request($vo);

        $filename = strrchr($regjpg[1][$i], "/") . ".jpg";

        file_put($rootweb, "/images", $filename, $temp);

        $html = str_replace($vo, "/images" . $filename, $html);

    } else {
        $temp = curl_request($url . $vo);
        $filename = strrchr($regjpg[1][$i], "/") . ".jpg";

        file_put($rootweb, "/images", $filename, $temp);

        $html = str_replace($vo, "/images" . $filename, $html);


    }
}

// png开始
$regpng = zhengzetiqu($html, $regaxpng);
$regpng[0] = array_unique($regpng[0]);
$regpng[1] = array_unique($regpng[1]);
if (!empty($regpng[0])) {
    for ($i = 0; $i < count($regpng[0]); $i++) {
        $vo = getBetween($regpng[0][$i], "\"", "\"");
        if (strpos($vo, "http") === 0) {
            $temp = curl_request($vo);

            $filename = strrchr($regpng[1][$i], "/") . ".png";

            file_put($rootweb, "/images", $filename, $temp);

            $html = str_replace($vo, "/images" . $filename, $html);

        } else {
            $temp = curl_request($url . $vo);
            $filename = strrchr($regpng[1][$i], "/") . ".png";

            file_put($rootweb, "/images", $filename, $temp);

            $html = str_replace($vo, "/images" . $filename, $html);


        }
    }
}


// gif开始
$reggif = zhengzetiqu($html, $regaxgif);
$reggif[0] = array_unique($reggif[0]);
$reggif[1] = array_unique($reggif[1]);
for ($i = 0; $i < count($reggif[0]); $i++) {
    $vo = getBetween($reggif[0][$i], "\"", "\"");
    if (strpos($vo, "http") === 0) {
        $temp = curl_request($vo);

        $filename = strrchr($reggif[1][$i], "/") . ".png";

        file_put($rootweb, "/images", $filename, $temp);

        $html = str_replace($vo, "/images" . $filename, $html);

    } else {
        $temp = curl_request($url . $vo);
        $filename = strrchr($reggif[1][$i], "/") . ".png";

        file_put($rootweb, "/images", $filename, $temp);

        $html = str_replace($vo, "/images" . $filename, $html);
    }
}



file_put($rootweb,"",$houzui,$html);